#ifndef __SRTP_SESSION_H
#define __SRTP_SESSION_H
#include <srtp2/srtp.h>
#include <vector>
#include <string>
namespace xrtc
{
    class SrtpSession
    {

    public:
        SrtpSession();
        ~SrtpSession();
        bool SetSend(int cs, const uint8_t *key, size_t key_len, const std::vector<int> &extension_ids);
        bool UpdateSend(int cs, const uint8_t *key, size_t key_len, const std::vector<int> &extension_ids);
        bool SetRecv(int cs, const uint8_t *key, size_t key_len, const std::vector<int> &extension_ids);
        bool UpdateRecv(int cs, const uint8_t *key, size_t key_len, const std::vector<int> &extension_ids);
        bool UnprotectRtp(void *p, int in_len, int *out_len);
        bool UnprotectRtcp(void *p, int in_len, int *out_len);
        bool ProtectRtp(void *p, int in_len, int max_len, int *out_len);
        bool ProtectRtcp(void *p, int in_len, int max_len, int *out_len);
        void GetAuthTagLen(int *rtp_auth_tag_len, int *rtcp_auth_tag_len);

    private:
        bool SetKey(int type, int cs, const uint8_t *key, size_t key_len, const std::vector<int> &extension_ids);
        static bool IncrementLibsrtpUageCountAndMaybeInit();
        static void DecrementLibsrtpUsageAndMaybeDeinit();

        void HandleEvent(srtp_event_data_t *ev);
        static void EventHandleThunk(srtp_event_data_t *ev);
        bool DoSetKey(int type, int cs, const uint8_t *key, size_t key_len, const std::vector<int> &extension_ids);
        bool UpdateKey(int type, int cs, const uint8_t *key, size_t key_len, const std::vector<int> &extension_ids);

    private:
        srtp_ctx_t *session_ = nullptr;
        bool inited_ = false;
        int rtp_auth_tag_len_ = 0;
        int rtcp_auth_tag_len_ = 0;
    };

} // namepsace xrtc

#endif